home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / network / lattice / portlib.lzh / PORTLIB / IOCTL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-10  |  4.8 KB  |  219 lines

  1. /*
  2.  * ioctl() emulation for MiNT; written by Eric R. Smith and placed
  3.  * in the public domain
  4.  */
  5.  
  6. #include <errno.h>
  7. #include <mintbind.h>
  8. #include <ioctl.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <linea.h>    /* for TIOCGWINSZ under TOS */
  12. #include <support.h>
  13. #include <stat.h>
  14. #include "lib.h"    /* for __open_stat */
  15.  
  16. extern int __mint;    /* MiNT version */
  17. extern int _ttydisc;
  18. extern int _ldisc;
  19.  
  20. /* in read.c */
  21. extern struct tchars __tchars;
  22. extern struct ltchars __ltchars;
  23.  
  24. int _ioctl(fd, cmd, arg)
  25.     int fd, cmd;
  26.     void *arg;
  27. {
  28.     long r;
  29.     int istty = isatty(fd);
  30.     struct sgttyb *sg = (struct sgttyb *) arg;
  31.     int null_fd;
  32.     long baud;
  33.  
  34.     if (istty) {
  35.         switch (cmd) {
  36.         case TIOCGETD:
  37.             *((int *)arg) = _ttydisc;
  38.             return 0;
  39.             break;
  40.         case TIOCSETD:
  41.             _ttydisc = *((int *)arg);
  42.             return 0;
  43.             break;
  44.         case TIOCLGET:
  45.             *((int *)arg) = _ldisc;
  46.             return 0;
  47.             break;
  48.         case TIOCLSET:
  49.             _ldisc = *((int *)arg);
  50.             return 0;
  51.             break;
  52.         case TIOCSWINSZ:
  53.             if (__mint < 9)
  54.                 return 0;
  55.             break;
  56.         case TIOCGWINSZ:
  57.             if (__mint < 9) {
  58.                 struct winsize *win = (struct winsize *)arg;
  59. #ifndef __SOZOBON__
  60.                 (void)linea0();
  61. #else /* __SOZOBON__ */
  62.                 linea0();
  63. #endif /* __SOZOBON__ */
  64.                 win->ws_row = V_CEL_MY + 1;
  65.                 win->ws_col = V_CEL_MX + 1;
  66.                 win->ws_xpixel = V_X_MAX;
  67.                 win->ws_ypixel = V_Y_MAX;
  68.                 return 0;
  69.             }
  70.             break;
  71. #ifdef __MINT__
  72.         case TIOCNOTTY:
  73.             if (__mint) {
  74.                 if ((fd < 0) || !(_isctty(fd))) {
  75.                     errno = EBADF;
  76.                     return -1;
  77.                 }
  78.                 (void) Fclose(fd);
  79.                 null_fd = (int) Fopen(__mint < 9 ? "V:\\null"
  80.                         : "U:\\dev\\null", O_RDWR);
  81.                 (void) Fforce(-1, null_fd);
  82.                 __open_stat[__OPEN_INDEX(-1)].status =
  83.                     FH_UNKNOWN;
  84.                 __open_stat[__OPEN_INDEX(fd)].status =
  85.                     FH_UNKNOWN;
  86.                 if (null_fd != fd) {
  87.                     (void) Fforce(fd, null_fd);
  88.                     (void) Fclose(null_fd);
  89.                 }
  90.                 return 0;
  91.             }
  92.             break;
  93. #endif /* __MINT__ */
  94.         default:
  95.             break;
  96.         }
  97.     }
  98.  
  99.     if (__mint) {
  100.       switch (cmd) {
  101.         case TIOCCDTR:
  102.           baud = 0;
  103.           r = Fcntl(fd, &baud, TIOCOBAUD);
  104.           if (r < 0) {
  105.             errno = (int) -r;
  106.         return -1;
  107.           }
  108.           return 0;
  109.           break;
  110.         case TIOCSDTR:
  111.           baud = -1;
  112.           r = Fcntl(fd, &baud, TIOCOBAUD);
  113.           if (r < 0) {
  114.             errno = (int) -r;
  115.         return -1;
  116.           }
  117.           r = Fcntl(fd, &baud, TIOCOBAUD);
  118.           if (r < 0) {
  119.             errno = (int) -r;
  120.         return -1;
  121.           }
  122.           return 0;
  123.           break;
  124.         case TIOCMGET:
  125.           if (__mint >= 0x10a) {
  126.                 char g;
  127.                 long ssp;
  128.                 short *mfp;
  129.                 short m;
  130.                 struct stat sb;
  131.                 long *msig;
  132.  
  133.                 msig = (long *) arg;
  134.                 r = Fcntl(fd, (long)&sb, FSTAT);
  135.                 if (r < 0) {
  136.           errno = -r;
  137.                   return -1;
  138.         }
  139.                 if (((sb.st_mode & S_IFMT) == S_IFCHR) && (sb.st_rdev == 257))
  140.                 {
  141.                   *msig = TIOCM_DSR;
  142.                   g = Giaccess(0, 14);
  143.                   *msig |= ((g & (1 << 3)) ? 0 : TIOCM_RTS);
  144.                   *msig |= ((g & (1 << 4)) ? 0 : TIOCM_DTR);
  145.                   mfp = ((short *) 0xfffffa00L);
  146.                   ssp = Super(0L);
  147.                   m = *mfp & 0xff;
  148.                   Super(ssp);
  149.                   *msig |= ((m & (1 << 1)) ? 0 : TIOCM_CAR);
  150.                   *msig |= ((m & (1 << 2)) ? 0 : TIOCM_CTS);
  151.                   *msig |= ((m & (1 << 6)) ? 0 : TIOCM_RNG);
  152.                   return 0;
  153.                 }
  154.                 errno = EINVAL;
  155.                 return -1;
  156.                 break;
  157.           }
  158.         case TIOCSETP:
  159.           if (__mint <= 0x10a) {
  160.         r = Fcntl(fd, arg, cmd);
  161.         if (r != -EINVAL)
  162.           break;
  163.         cmd = TIOCSETN;
  164.           }
  165.           /*FALLTHRU*/
  166.         default:
  167.           r = Fcntl(fd, arg, cmd);
  168.           break;
  169.         }
  170.     }
  171.     else if (istty) {
  172.         r = 0;
  173.         switch(cmd) {
  174.         case TIOCSETP:
  175.             fd = __OPEN_INDEX(fd);
  176.             if (fd < 0 || fd >= __NHANDLES)
  177.                 fd = __NHANDLES - 1;
  178.             __open_stat[fd].flags = sg->sg_flags;
  179.             break;
  180.         case TIOCGETP:
  181.             fd = __OPEN_INDEX(fd);
  182.             if (fd < 0 || fd >= __NHANDLES)
  183.                 fd = __NHANDLES - 1;
  184.             sg->sg_flags = __open_stat[fd].flags;
  185.             sg->sg_ispeed = sg->sg_ospeed = 0;
  186.             sg->sg_erase = 'H' & 0x1f;
  187.             sg->sg_kill = 'U' & 0x1f;
  188.             break;
  189.         case TIOCGETC:
  190.             *((struct tchars *)arg) = __tchars;
  191.             break;
  192.         case TIOCSETC:
  193.             __tchars = *((struct tchars *)arg);
  194.             break;
  195.         case TIOCGLTC:
  196.             *((struct ltchars *)arg) = __ltchars;
  197.             break;
  198.         case TIOCSLTC:
  199.             __ltchars = *((struct ltchars *)arg);
  200.             break;
  201.         case TIOCGPGRP:
  202.             *((long *)arg) = 0;
  203.             break;
  204.         case TIOCSPGRP:
  205.             break;
  206.         default:
  207.             r = -EINVAL;
  208.         }
  209.     }
  210.     else
  211.         r = -EINVAL;
  212.  
  213.     if (r < 0) {
  214.         errno = (int) -r;
  215.         return -1;
  216.     }
  217.     return (int) r;
  218. }
  219.